package com.king.first.app.windows;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.WindowAssigner;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.api.windowing.windows.Window;
import org.apache.flink.util.Collector;

import java.util.Arrays;

/**
 * 滚动窗口
 */
public class Tumbling_Windows {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> socketTextStream = env.socketTextStream("hadoop01", 9999);

        SingleOutputStreamOperator<Tuple2<String, Integer>> flatMapStream =
                socketTextStream.flatMap((s, collector) -> {
                    String[] s1 = s.split(" ");
                    Arrays.stream(s1).forEach(word -> collector.collect(Tuple2.of(word, 1)));
                });

        KeyedStream<Tuple2<String, Integer>, String> keyedStream =
                flatMapStream.keyBy(tuple2 -> tuple2.f0);

        WindowedStream<Tuple2<String, Integer>, String, TimeWindow> windowedStream =
                keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(4)));

        SingleOutputStreamOperator<Tuple2<String, Integer>> sum = windowedStream.sum(1);

        keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(10))).aggregate(new AggregateFunction<Tuple2<String, Integer>, Object, Object>() {
            @Override
            public Object createAccumulator() {
                return null;
            }

            @Override
            public Object add(Tuple2<String, Integer> value, Object accumulator) {
                return null;
            }

            @Override
            public Object getResult(Object accumulator) {
                return null;
            }

            @Override
            public Object merge(Object a, Object b) {
                return null;
            }
        }, new WindowFunction<Object, Object, String, TimeWindow>() {
            @Override
            public void apply(String s, TimeWindow window, Iterable<Object> input, Collector<Object> out) throws Exception {

            }
        });

        sum.print();

        env.execute();
    }
}

